package cn.edu.szpt.qqdemov3.net;

import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class MyHttpUrlConnectionT1 implements Runnable {

    private String url;
    private BufferedReader bufferedReader;
    private HttpURLConnection urlConnection;
    private String method;


//    @Override
//    protected Result doInBackground(Object... objects) {
//
//        Result<Object> objectResult = new Result<>();
//
//
//        try {
//            urlConnection = (HttpURLConnection) new URL(url).openConnection();
//
//
//            urlConnection.setRequestMethod(method);
//
//            urlConnection.setConnectTimeout(3000);
//            urlConnection.setRequestProperty("Content-Type", "application/json");
////            urlConnection.setRequestProperty("charter", "utf-8");
//            urlConnection.connect();
//
//            int responseCode = urlConnection.getResponseCode();
//
//            if (responseCode == 200) {
//                StringBuilder stringBuffer = new StringBuilder();
//                bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
//
//                String line;
//                if (null != (line = bufferedReader.readLine())) {
//                    stringBuffer.append(line);
//                }
//
//                bufferedReader.close();
//
//                urlConnection.disconnect();
//
//            }
//
//
//        } catch (MalformedURLException e) {
//            e.printStackTrace();
//        } catch (IOException e) {
//            e.printStackTrace();
//        } finally {
//            try {
//                if (bufferedReader != null) {
//                    bufferedReader.close();
//                }
//
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
//
//
//            if (urlConnection != null) {
//                urlConnection.disconnect();
//            }
//
//
//        }
//
//
//        return objectResult;
//    }

    public void doGet(String url) {


        this.url = url;
        this.method = "get";

    }

    public void doPost() {


        this.method = "get";
//        execute();


    }

    Handler handler;

    public void setHandler(Handler handler) {
        this.handler = handler;
    }

    @Override
    public void run() {


//        Result<Object> objectResult = new Result<>();


        try {
            url = "http://8.129.167.217/SoccerDataHandler.ashx?action=getTeamStr";
            urlConnection = (HttpURLConnection) new URL(url).openConnection();


            urlConnection.setRequestMethod("GET");

            urlConnection.setConnectTimeout(10000);
            urlConnection.setRequestProperty("Content-Type", "application/json");
//            urlConnection.setRequestProperty("charter", "utf-8");
            urlConnection.connect();

            int responseCode = urlConnection.getResponseCode();

            try {
                Thread.sleep(3000);
                Log.d("TAG", "run in : "+Thread.currentThread());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            if (responseCode == 200) {
                StringBuilder stringBuffer = new StringBuilder();
                bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));

                String line;
                if (null != (line = bufferedReader.readLine())) {
                    stringBuffer.append(line);
                }

                System.out.println(stringBuffer.toString());

                Message msg = new Message();
                Bundle data = new Bundle();
                data.putString("data", stringBuffer.toString());
                msg.setData(data);
                handler.sendMessage(msg);

                bufferedReader.close();

                urlConnection.disconnect();

            }


        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (bufferedReader != null) {
                    bufferedReader.close();
                }

            } catch (IOException e) {
                e.printStackTrace();
            }


            if (urlConnection != null) {
                urlConnection.disconnect();
            }


        }






    }
}
